<?php
header('Content-Type: text/html; charset=ISO-8859-1');
header('Access-Control-Allow-Origin: *');
if ($_SERVER['SERVER_NAME'] == "localhost") {
    include "../../cvonline/admin/config/define.php";
} else {
    include "../../admin/config/define.php";
}


include_once("_onStartPage.php");
include_once("_clsAllAction.php");
require 'Kernel.php';
$caminhoModulo = $caminhoAdmin . $PATH_SITE;
$caminhoModuloAbsoluto = $caminhoAbsolutoAdmin . $PATH_SITE;


$DEL = true;
include_once($caminhoModulo . "configField.inc.php");


$objAllAction = new clsAllAction()
?>
<link rel="stylesheet" href="<?php echo PATH_CSS ?>admin.css" type="text/css">
<script language="JavaScript" src="<?= PATH_SCRIPTS ?>jQuery/jquery-latest.js" type="text/javascript"></script>
<script language="JavaScript" src="<?= PATH_SCRIPTS ?>jQuery/jquery.livequery.js" type="text/javascript"></script>
<?
if (file_exists($caminhoModulo . "include_jscript.php")) {
    include($caminhoModulo . "include_jscript.php");
}
?>
<script>

    function showInsert(path){
        window.open(path,"","height=350,width=620,scrollbars=yes");
    }

    $(document).ready(function(){

        $(".delete_grid").click(function(){
	
            if(confirm("Tem certeza que deseja remover este(s) dado(s) ?")){
                $(this).parent().parent().remove();
                var  keyVal = $(this).parent().parent().attr("keyVal");
                $.ajax({
                    type: "POST",
                    url: "acoes_ajax.php",
                    dataType: "html",
                    data: "acao=3&keyName=<?= $KEY ?>&keyValue="+keyVal+"&tableRel=<?= $TABLE ?>",
                    success: function( result ){
                        alert(result);
                    }
                });
            }else{
                return false;
            }
        });

    });
</script>
<?
if ($ADD) {
    ?>
    <div style="cursor:pointer;width:80px;" onclick="showInsert('<?= $caminhoModuloAbsoluto ?>adicionar.php?size=mini&keyValue=<?= $_GET['keyValue'] ?>&extraGrid=<?= urlencode(stripslashes($_GET['extraGrid'])) ?>')">
        <img alt="excluir" title="excluir" src="<?= PATH_IMGS ?>add.gif" border="0" />Adicionar 
    </div>
    <?
}
?>
<table width="100%" border="1" class="tabelaSimples">
    <tr>
        <?
        if ($DEL) {
            ?>
            <td align="center" style="color:#996600">Excluir</td>
            <?
        }

        for ($j = 0; $j < count($campos); $j++) {
            if ($campos[$j][1] != $KEY && $campos[$j][1] && $campos[$j][2] != "FK_HIDDEN" && $campos[$j][2] != "MASTER_DETAIL") {
                ?>	
                <td align="center" style="color:#996600"><?= $campos[$j][0] ?></td>
                <?
            }
        }
        if (file_exists($caminhoModulo . "include_header_list.php")) {
            include($caminhoModulo . "include_header_list.php");
        }
        ?>			
    </tr>

    <?
    if (!$sqlAdicional) {
        $objKernel = new Kernel();
        $objKernel->setCampos($campos);
        $objKernel->setTable($TABLE);
        $objKernel->setPrimaryKeyTable($ID);
        $objKernel->setAditionalWhereClause($ADITIONAL_WHERE_CLAUSE);
        $objKernel->setParentTable($PARENT_TABLE);
        $queryBase = $objKernel->montarListaSQL();
    } else {
        $queryBase = $sqlAdicional . " AND " . base64_decode($_GET['keyName']) . " = " . base64_decode($_GET['keyValue']);
    }
    $queryBase .= " ORDER BY " . $KEY . " DESC ";

    $rs = $objDB->DBExecutarSQL($queryBase);
    $rsDados = $objDB->DBDadosRegistros($rs);

    foreach ($rsDados as $rsDado) {
        ?>
        <tr keyVal="<?= $rsDado->$KEY ?>" <? if ($EDIT) { ?> onclick="showInsert('<?= $caminhoModuloAbsoluto ?>adicionar.php?size=mini&<?= $KEY ?>=<?= $rsDado->$KEY ?>&keyValue=<?= $_GET['keyValue'] ?>')" style="cursor:pointer" <? } ?>>
            <?
            if ($DEL) {
                ?>		
                <td width="5%" align="center">
                    <input type="checkbox" />
                    <!-- <img style="cursor:pointer" class="delete_grid" alt="excluir" title="excluir" src="<?= PATH_IMGS ?>excluir.gif" border="0" /> 
                    --></td>
                <?
            }
            for ($j = 0; $j < count($campos); $j++) {
                if ($campos[$j][1] != $KEY && $campos[$j][2] != "FK_HIDDEN") {
                    if ($campos[$j][2] == "LOV2") {
                        $FIELD_WHERE_VAL = substr($campos[$j][20], strpos($campos[$j][20], ".") + 1, strlen($campos[$j][20]));
                        $rs = $objAllAction->getLov2($campos[$j][7], $campos[$j][6], $campos[$j][1], $campos[$j][20], $rsDado->$FIELD_WHERE_VAL);
                        $dados = $objDB->DBDadosRegistros($rs);
                        foreach ($dados as $dado) {
                            ?>
                            <td>
                                <?
                                $k = 0;
                                foreach ($campos[$j][7] as $FIELDS_SELECT) {
                                    $FIELD_ALIAS = $FIELDS_SELECT . $k;
                                    print $dado->$FIELD_ALIAS . " ";
                                    $k++;
                                }
                                ?>
                            </td>					
                            <?
                        }
                    } else if ($campos[$j][2] == "LOV") {
                        $rs = $objAllAction->getListaLov($campos[$j][1], $campos[$j][7], $campos[$j][6]);
                        $dados = $objDB->DBDadosRegistros($rs);
                        foreach ($dados as $dado) {
                            if ($dado->$campos[$j][1] == $rsDado->$campos[$j][1]) {
                                ?>
                                <td><?= $dado->$campos[$j][7] ?></td>
                                <?
                            }
                        }
                    } else if ($campos[$j][2] == "DATE_TIME") {
                        ?>
                        <td><?php
                                if($rsDado->$campos[$j][1] != ''){
                                    echo date("d/m/Y", strtotime($rsDado->$campos[$j][1]));
                                }
                            ?></td>
                        
                        

                        <?
                    } else if ($campos[$j][2] == "TEXT" || $campos[$j][2] == "LONGTEXT") {
                        ?>
                        <td><? echo $rsDado->$campos[$j][1] ?></td>
                        <?
                    } else if ($campos[$j][2] == "MOEDA") {
                         ?>
                        <td><?= number_format($rsDado->$campos[$j][1], 2, ',', '.') ?></td>
                        <?
                    } else if ($campos[$j][2] == "SELECT") {
                        $SELECT_VALUES = $campos[$j][12];
                        foreach ($SELECT_VALUES as $key => $display) {
                            if ($key == $rsDado->$campos[$j][1]) {
                                ?>
                                <td><?= $display ?></td>
                                <?
                            }
                        }
                    }
                }
            }
            if (file_exists($caminhoModulo . "include_value_list.php")) {
                include($caminhoModulo . "include_value_list.php");
            }
            ?>
        <tr>
            <?
        }
        ?>
    </tr>	
</table>